﻿Public Class perEmpresa
    Inherits PersistenciaPadrao.AcessoBD

#Region "Métodos"

    Public Function validarEmpresa(ByVal iCnpj As Int32,
                                   ByVal iCodEmpresa As Int32) As Boolean

        Dim sSql As String

        sSql = " SELECT " & Environment.NewLine &
               "     seg_empresa.* " &
               " FROM  " & Environment.NewLine &
               "     seg_empresa " & Environment.NewLine &
               " WHERE " & Environment.NewLine &
               "     codEmpresa = @codEmpresa " & Environment.NewLine &
               " AND cnpj = @cnpj "

        MyBase.limparParametros()
        MyBase.inserirParametro("@codEmpresa", iCodEmpresa)
        MyBase.inserirParametro("@cnpj", iCnpj)

        Return MyBase.executarConsulta(sSql, True).Rows.Count = 0

    End Function

    Public Function validarExclusaoEmpresa(ByVal iCodEmpresa As Int32) As Boolean

        Dim sSql As String

        sSql = " SELECT " & Environment.NewLine &
               "    seg_empresa.codEmpresa " &
               " FROM  " & Environment.NewLine &
               "     seg_empresa " & Environment.NewLine &
               " WHERE " & Environment.NewLine &
               "    seg_empresa.codEmpresa = @codEmpresa "

        MyBase.limparParametros()
        MyBase.inserirParametro("@codEmpresa", iCodEmpresa)

        Return MyBase.executarConsulta(sSql, True).Rows.Count = 0

    End Function

    Public Function inserirEmpresa(ByVal sRazaoSocial As String,
                                   ByVal sNomeFantasia As String,
                                   ByVal iCodEndereco As Int32,
                                   ByVal dCNPJ As Double,
                                   ByVal sInscMunicipal As String,
                                   ByVal sInscEstadual As String,
                                   ByVal sTelefone As String,
                                   ByVal sEmail As String,
                                   ByVal sSite As String,
                                   ByVal byLogo() As Byte,
                                   ByVal sNumero As String,
                                   ByVal sComplemento As String) As Int32
        Dim sSql As String

        sSql = " INSERT INTO seg_empresa " & Environment.NewLine &
               " ( " & Environment.NewLine &
               "    razaoSocial," & Environment.NewLine &
               "    nomeFantasia," & Environment.NewLine &
               "    codEndereco," & Environment.NewLine &
               "    cnpj," & Environment.NewLine &
               "    inscMunicipal," & Environment.NewLine &
               "    inscEstadual," & Environment.NewLine &
               "    telefone," & Environment.NewLine &
               "    email," & Environment.NewLine &
               "    site, " & Environment.NewLine &
               "    logo," & Environment.NewLine &
               "    numero," & Environment.NewLine &
               "    complemento" & Environment.NewLine &
               " )" & Environment.NewLine &
               " VALUES" & Environment.NewLine &
               " (" & Environment.NewLine &
               "    @razaoSocial " & Environment.NewLine &
               "    ,@nomeFantasia " & Environment.NewLine &
               "    ,@codEndereco " & Environment.NewLine &
               "    ,@cnpj " & Environment.NewLine &
               "    ,@inscMunicipal " & Environment.NewLine &
               "    ,@inscEstadual " & Environment.NewLine &
               "    ,@telefone " & Environment.NewLine &
               "    ,@email" & Environment.NewLine &
               "    ,@site " & Environment.NewLine &
               "    ,@logo" & Environment.NewLine &
               "    ,@numero " & Environment.NewLine &
               "    ,@complemento " & Environment.NewLine &
               " ); SELECT SCOPE_IDENTITY()"

        MyBase.limparParametros()
        MyBase.inserirParametro("@razaoSocial", sRazaoSocial)
        MyBase.inserirParametro("@nomeFantasia", sNomeFantasia)
        MyBase.inserirParametro("@codEndereco", iCodEndereco)
        MyBase.inserirParametro("@cnpj", dCNPJ)
        MyBase.inserirParametro("@inscMunicipal", sInscMunicipal)
        MyBase.inserirParametro("@inscEstadual", sInscEstadual)
        MyBase.inserirParametro("@telefone", sTelefone)
        MyBase.inserirParametro("@email", sEmail)
        MyBase.inserirParametro("@site", sSite)
        MyBase.inserirParametro("@logo", byLogo)
        MyBase.inserirParametro("@numero", sNumero)
        MyBase.inserirParametro("@complemento", sComplemento)

        Return MyBase.executarConsultaCampo(sSql, True)

    End Function

    Public Sub editarEmpresa(ByVal sCodigo As Int32,
                             ByVal sRazaoSocial As String,
                             ByVal sNomeFantasia As String,
                             ByVal iCodEndereco As Int32,
                             ByVal dCNPJ As Double,
                             ByVal sInscMunicipal As String,
                             ByVal sTelefone As String,
                             ByVal sEmail As String,
                             ByVal sSite As String,
                             ByVal sInscEstadual As String,
                             ByVal byLogo() As Byte,
                             ByVal sNumero As String,
                             ByVal sComplemento As String)

        Dim sSql As String

        sSql = " UPDATE seg_empresa SET " & Environment.NewLine &
                "    razaoSocial = @razaoSocial," & Environment.NewLine &
                "    nomeFantasia = @nomeFantasia," & Environment.NewLine &
                "    codEndereco = @codEndereco," & Environment.NewLine &
                "    cnpj = @cnpj," & Environment.NewLine &
                "    inscEstadual = @inscEstadual," & Environment.NewLine &
                "    inscMunicipal = @inscMunicipal," & Environment.NewLine &
                "    telefone = @telefone," & Environment.NewLine &
                "    email = @email," & Environment.NewLine &
                "    site = @site, " & Environment.NewLine &
                "    logo = @logo," & Environment.NewLine &
                "    numero = @numero," & Environment.NewLine &
                "    complemento = @complemento" & Environment.NewLine &
                " WHERE codEmpresa = @codEmpresa"

        MyBase.limparParametros()
        MyBase.inserirParametro("@codEmpresa", sCodigo)
        MyBase.inserirParametro("@razaoSocial", sRazaoSocial)
        MyBase.inserirParametro("@nomeFantasia", sNomeFantasia)
        MyBase.inserirParametro("@codEndereco", iCodEndereco)
        MyBase.inserirParametro("@cnpj", dCNPJ)
        MyBase.inserirParametro("@inscEstadual", sInscEstadual)
        MyBase.inserirParametro("@inscMunicipal", sInscMunicipal)
        MyBase.inserirParametro("@telefone", sTelefone)
        MyBase.inserirParametro("@email", sEmail)
        MyBase.inserirParametro("@site", sSite)
        MyBase.inserirParametro("@logo", byLogo)
        MyBase.inserirParametro("@numero", sNumero)
        MyBase.inserirParametro("@complemento", sComplemento)

        MyBase.executarAcao(sSql, True)

    End Sub

    Public Sub excluirEmpresa(ByVal iCodEmpresa As Int32)

        Dim sSql As String

        sSql = " DELETE FROM " & Environment.NewLine &
               "    seg_empresa " & Environment.NewLine &
               " WHERE " & Environment.NewLine &
               "    codEmpresa = @codEmpresa; "

        MyBase.limparParametros()
        MyBase.inserirParametro("@codEmpresa", iCodEmpresa)

        MyBase.executarAcao(sSql, True)

    End Sub


    Public Function selecionarEmpresaCodigo(ByVal iCodEmpresa As Int32) As DataRow

        Dim sSql As String
        Dim datDados As New DataTable



        sSql = " SELECT " & Environment.NewLine &
               "    seg_empresa.codEmpresa, " & Environment.NewLine &
               "    seg_empresa.logo, " & Environment.NewLine &
               "    seg_empresa.razaoSocial, " & Environment.NewLine &
               "    seg_empresa.nomeFantasia, " & Environment.NewLine &
               "    seg_empresa.codEndereco, " & Environment.NewLine &
               "    seg_empresa.cnpj, " & Environment.NewLine &
               "    seg_empresa.inscEstadual, " & Environment.NewLine &
               "    seg_empresa.inscMunicipal, " & Environment.NewLine &
               "    seg_empresa.telefone, " & Environment.NewLine &
               "    seg_empresa.email," & Environment.NewLine &
               "    seg_empresa.site, " & Environment.NewLine &
               "    seg_empresa.numero," & Environment.NewLine &
               "    seg_empresa.complemento" & Environment.NewLine &
               " FROM seg_empresa " & Environment.NewLine &
               " WHERE " & Environment.NewLine &
               "    codEmpresa = @codEmpresa "

        MyBase.limparParametros()
        MyBase.inserirParametro("@codEmpresa", iCodEmpresa)

        datDados = MyBase.executarConsulta(sSql, True)
        If datDados.Rows.Count > 0 Then
            Return datDados.Rows(0)
        Else
            Return Nothing
        End If

    End Function

    Public Function selecionarEmpresa(ByVal iCodEmpresa As Int32) As DataTable


        Dim sSql As String
        Dim datDados As New DataTable



        sSql = " SELECT " & Environment.NewLine &
               "    codEmpresa " & Environment.NewLine &
               "    ,razaoSocial " & Environment.NewLine &
               "    ,nomeFantasia " & Environment.NewLine &
               " FROM seg_empresa " & Environment.NewLine
        If (iCodEmpresa > 0) Then

            sSql &= " WHERE " & Environment.NewLine &
                    "    codEmpresa = @codEmpresa "

        End If

        MyBase.limparParametros()
        MyBase.inserirParametro("@codEmpresa", iCodEmpresa)

        datDados = MyBase.executarConsulta(sSql, True)

        Return datDados

    End Function



    Public Function retornarEmissorPadrao() As Int32
        Dim sSql As String

        sSql = "SELECT codEmpresa " & Environment.NewLine &
               "  FROM seg_empresa"

        Return MyBase.executarConsultaCampo(sSql, True)

    End Function

#End Region

End Class
